﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class FProduct : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bindata();
        }
    }
    public void Bindata()
    {
        ProductBOL pBOL = new ProductBOL();
        GridView1.DataSource = pBOL.LoadProduct();
        GridView1.DataBind();
    }

    public void UpdateRecord(object s, GridViewUpdateEventArgs e)
    {
        GridViewRow row = GridView1.Rows[e.RowIndex];
        TextBox tProductID = (TextBox)row.FindControl("txtProductID");
        TextBox tProductName = (TextBox)row.FindControl("txtProductName");
        TextBox tCategory = (TextBox)row.FindControl("txtCategory");
        TextBox tUnitPrice = (TextBox)row.FindControl("txtUnitPrice");
        TextBox tQuantityInStock = (TextBox)row.FindControl("txtQuantityInStock");
        TextBox timage = (TextBox)row.FindControl("txtimages");

        Product p = new Product();
        ProductBOL pBOL = new ProductBOL();
        try
        {
            p.ProductID = tProductID.Text;
            p.ProductName = tProductName.Text;
            p.Category = tCategory.Text;
            p.UnitPrice = Convert.ToDouble(tUnitPrice.Text);
            p.QuantityInStock = Convert.ToInt32(tQuantityInStock.Text);
            p.Images = timage.Text;
            int result = pBOL.updateProduct(p);
            if (result > 0)
            {
                LResult.Text = "Update record is successful!";
            }
            else
                LResult.Text = "Update record is error";
        }
        catch (Exception ex)
        {
            LResult.Text = ex.Message;
        }
        finally
        {

            p = null;
            pBOL = null;
        }
        GridView1.EditIndex = -1;
        Bindata();
    }
    protected void EditRecord(object s, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        Bindata();
    }
    protected void CancelRecord(object s, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        Bindata();
    }
    protected void DeleteRecord(object s, GridViewDeleteEventArgs e)
    {
        string tProductID = GridView1.DataKeys[e.RowIndex].Value.ToString();

        Product p = new Product();
        ProductBOL pBOL = new ProductBOL();
        try
        {
            p.ProductID = tProductID;

            int result = pBOL.deleteProduct(p);
            if (result > 0)
            {
                LResult.Text = "Delete record is successful.";
            }
            else
                LResult.Text = "Delete record is error.";
        }
        catch (Exception ex)
        {
            LResult.Text = ex.Message;
        }
        finally
        {

            p = null;
            pBOL = null;
        }
        GridView1.EditIndex = -1;
        Bindata();
    }
}